package com.google.fbyoutube.client.widget;

import java.util.List;

import com.google.fbyoutube.client.page.Home;
import com.google.fbyoutube.shared.FeedUrlMaker;
import com.google.fbyoutube.shared.ytVideoEntry;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockPanel;

public class Tabhome extends Composite {

	public Tabhome() {

		DockPanel dockPanel = new DockPanel();
		initWidget(dockPanel);
		dockPanel.setSize("100%", "100%");

		palSortBy = new PalSortBy();
		dockPanel.add(palSortBy, DockPanel.NORTH);

		palMainCell = new PalMainCell();
		dockPanel.add(palMainCell, DockPanel.CENTER);
		
		palTrends = new PalTrends();
		dockPanel.add(palTrends, DockPanel.SOUTH);
		
		loadTrends();
	}

	public void loadVideos(String url) {
		Home.videoService.getVideos(url,
				new AsyncCallback<List<ytVideoEntry>>() {

					@Override
					public void onSuccess(List<ytVideoEntry> result) {
						palMainCell.reload(result);
						loadComplte = true;
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO: Display error messages here

					}
				});
	}
	
	public void loadTrends() {
		Home.videoService.getVideos(FeedUrlMaker.buildUrl(FeedUrlMaker.TREND_FEED, null, null, null),
				new AsyncCallback<List<ytVideoEntry>>() {

					@Override
					public void onSuccess(List<ytVideoEntry> result) {
						palTrends.loadTrendVideo(result);
					}

					@Override
					public void onFailure(Throwable caught) {
						// TODO: Display error messages here
					}
				});
	}
	
	public boolean isLoadComplte() {
		return loadComplte;
	}
	
	public void enableVideoPaging(boolean enabled) {
		this.palSortBy.setShowPaging(enabled);
		
		// if paging is enabled, it is search result
		if (enabled) {
			palMainCell.setTitle("Search Results");
		} else {
			palMainCell.setTitle("Top Videos");
		}
	}

	private PalMainCell palMainCell;
	private PalSortBy palSortBy;
	private PalTrends palTrends;
	private boolean loadComplte = false;

}
